package errorx

import (
	"net/http"
)

type SimpleMsg struct {
	Msg string `json:"msg"`
}

type ApiError struct {
	Code      int    `json:"code"`
	ErrorCode int    `json:"errorCode"`
	Msg       string `json:"msg"`
}

type ApiErrorResponse struct {
	Code int             `json:"code"`
	Msg  string          `json:"msg"`
	Data ErrorDetailData `json:"data"`
}

func NewApiError(code int, msg string) error {
	return &ApiError{Code: code, Msg: msg}
}

func NewApiErrorWithoutMsg(code int) error {
	return &ApiError{Code: code, Msg: ""}
}

func NewApiInternalError(msg string) error {
	return &ApiError{Code: http.StatusInternalServerError, Msg: msg}
}

func NewApiBadRequestError(msg string) error {
	return &ApiError{Code: http.StatusBadRequest, Msg: msg}
}

func NewApiUnauthorizedError(msg string) error {
	return &ApiError{Code: http.StatusUnauthorized, Msg: msg}
}

func NewApiForbiddenError(msg string) error {
	return &ApiError{Code: http.StatusForbidden, Msg: msg}
}

func NewApiNotFoundError(msg string) error {
	return &ApiError{Code: http.StatusNotFound, Msg: msg}
}

func NewApiBadGatewayError(msg string) error {
	return &ApiError{Code: http.StatusBadGateway, Msg: msg}
}

func (e *ApiError) WithCode(code int) *ApiError {
	e.Code = code
	return e
}

func (e *ApiError) WithErrorCode(errorCode int) *ApiError {
	e.ErrorCode = errorCode
	return e
}

func (e *ApiError) WithMsg(msg string) *ApiError {
	e.Msg = msg
	return e
}

func (e *ApiError) Error() string {
	return e.Msg
}

func (e *ApiError) Data() *ApiErrorResponse {
	return &ApiErrorResponse{
		Code: e.Code,
		Msg:  e.Msg,
		Data: ErrorDetailData{ErrorCode: e.ErrorCode, Detail: e.Error()},
	}
}

func (e *ApiError) DataWithDetail(detail string) *ApiErrorResponse {
	return &ApiErrorResponse{
		Code: e.Code,
		Msg:  e.Msg,
		Data: ErrorDetailData{ErrorCode: e.ErrorCode, Detail: detail},
	}
}
